package com.yx.cloud.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Toast;

import com.google.zxing.encoding.EncodingHandler;
import com.iot.base.service.req.NetworkConfigParam;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.constant.Constants;
import com.yx.cloud.databinding.QrcodeaddequimentActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class QrCodeAddEquipmentActivity extends BaseActivity implements View.OnClickListener {
    private QrcodeaddequimentActivityBinding binding;
    private String json;
    private String qrConfigToken = "", network = "";

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = QrcodeaddequimentActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        network = getIntent().getStringExtra("network");


        binding.ivQrCodeAddEquipmentReturn.setOnClickListener(this);
        binding.btnQRCodeNextStep.setOnClickListener(this);


        NetworkConfigParam param = new NetworkConfigParam();
        param.setSsid(Constants.wifi_Name);
        param.setPassword(Constants.wifi_Pass);
        AppContext.getAccountService().addDevice(param);
    }

    @Override
    public void onClick(View view) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_qrCodeAddEquipmentReturn:
                Intent_utils.backIntent(this);
                break;
            case R.id.btn_QRCodeNextStep:
                boolean checked = binding.cbConfirm.isChecked();
//                if (!checked) {
//                    ToastUtil.show(this, getString(R.string.Please_confirm_scanned), Toast.LENGTH_SHORT);
//                    return;
//                }
                Intent intent_qrCode = new Intent(this, DeviceConnectActivity2.class);
                intent_qrCode.putExtra("network", "qrCode");
                intent_qrCode.putExtra("qrConfigToken", qrConfigToken);
                startActivity(intent_qrCode);
                finish();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        if (event.getType() == Event.MESSAGE_TYPE_GET_QE_CONFIG_TOKEN) {
            if (event.isResult()) {
                qrConfigToken = (String) event.getContent();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("s", Constants.wifi_Name);
                    jsonObject.put("p", Constants.wifi_Pass);
                    jsonObject.put("t", qrConfigToken);
                    json = jsonObject.toString();
                    LogUtils.i("封装二维码 jsonObject :" + json);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
                int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
                LogUtils.i("屏幕宽高  :" + screenWidth + " , " + screenHeight);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.desktop_logo_icon, null);
                Bitmap mQrCodeBitmap = EncodingHandler.createQRCode(json, 800, 800, bitmap);
                binding.ivAddEquipmentQrCode.setImageBitmap(mQrCodeBitmap);
            } else {
                ToastUtil.show(this, event.getContent().toString(), Toast.LENGTH_LONG);
            }
        } else if (event.getType() == Event.MESSAGE_TYPE_GET_QE_BITMAP) {
            LogUtils.i(" 配网 二维码图片返回");
            Bitmap bitmap = (Bitmap) event.getContent();
            binding.ivAddEquipmentQrCode.setImageBitmap(bitmap);
        }
//        else if (event.getType() == Event.MESSAGE_TYPE_CONFIGNETWORK) {
//            if (event.isResult()) {
//                ToastUtil.show(this, "配网成功", Toast.LENGTH_SHORT);
//            } else {
//                ToastUtil.show(this, event.getContent().toString(), Toast.LENGTH_SHORT);
//            }
//        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
